package com.zwr.sheller.shop.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Seller;
import com.zwr.sheller.core.util.JwtUtil;
import com.zwr.sheller.shop.service.LoginService;
import com.zwr.sheller.shop.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: 18032
 * @Date: 11:20 2022/1/7
 */
@RestController
@CrossOrigin
@RequestMapping("/login")
@Api(tags = "后台登录")
public class LoginController {

    @Autowired
    LoginService loginService;
    @Autowired
    StoreService storeService;


    @PostMapping(value = "/loginSeller")
    @ApiOperation(value = "后台登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "pwd", value = "手机密码", dataType = "String", required = true),
    })
    public Map<String, Object> loginSeller(String phone, String pwd) throws WQQBusinessException {
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(pwd)){
            System.out.println(phone+"________"+pwd);
            throw new WQQBusinessException(WQQExceptionCode.WRONG_PHONE_OR_PWD);
        }
        Seller login = loginService.login(phone, pwd);

        //生成JWT给前端
        String token = JwtUtil.sign(phone, login.getStoreId(),login.getSellerId(),login.getMerchantId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("token",token);
        jsonResult.setCode("200");
        jsonResult.setField("msg","登陆成功");
        return jsonResult.getMap();
    }

    @PostMapping("/forgetSellerPwd")
    @ApiOperation("忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "pwd", value = "手机密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "code", value = "手机验证码", dataType = "String", required = true)
    })
    public Map<String, Object> forgetPwd(String phone,String pwd,String code) throws WQQBusinessException {
        Integer integer = loginService.forgetPwd(phone, code, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }
}
